package com.fowo.api.service.handleWork;

import com.fowo.api.entity.Product;
import com.fowo.api.model.logistics.order.LogisticsOrderUpdatedDeclareAction;
import com.fowo.api.model.logistics.order.LogisticsOrderVo;
import com.fowo.api.model.nav.sell.order.declare.NavSellOrderDeclareItemVo;
import com.fowo.api.model.nav.sell.order.declare.NavSellOrderDeclareVo;
import com.fowo.api.service.impl.LogisticsOrderServiceImpl;
import com.fowo.api.service.impl.NavSellOrderDeclareServiceImpl;
import com.fowo.api.service.impl.ProductServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class LogisticsOrderUpdatedDeclareActionService implements LogisticsOrderUpdatedDeclareAction {
    @Resource
    private LogisticsOrderServiceImpl logisticsOrderService;
    @Resource
    private NavSellOrderDeclareServiceImpl navSellOrderDeclareService;
    @Resource
    private ProductServiceImpl productService;

    @Override
    public void run(List<Long> idList) throws Exception {
        for(Long id : idList){
            LogisticsOrderVo logisticsOrderVo = logisticsOrderService.getVoById(id);
            List<NavSellOrderDeclareItemVo> declares = logisticsOrderVo.getDeclare();
            for(NavSellOrderDeclareItemVo declareItem : declares){
                final Product product = productService.getById(declareItem.getProductId());
                NavSellOrderDeclareVo model = new NavSellOrderDeclareVo();
                BeanUtils.copyProperties(declareItem,model);

                model.setChineseName(product.getChineseName());
                model.setEnglishName(product.getEnglishName());
                model.setCustomsCharges(product.getCustomsCharges());
                model.setPackingWeight(product.getPackingWeight());
                model.setCustomsCode(product.getCustomsCode());
                navSellOrderDeclareService.update(model);
            }
        }
    }
}
